package com.changgou.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.search.dao.ESManagerMapper;
import com.changgou.search.pojo.SkuInfo;
import com.changgou.search.service.EsManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

//elasticsearch搜索服务接口实现
@Service
public class ESManagerServiceImpl implements EsManagerService {

    @Autowired
    private ESManagerMapper esManagerMapper;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private ElasticsearchTemplate template;

    //创建索引库结构
    @Override
    public void createIndexAndMapping() {
        //创建索引
        template.createIndex(SkuInfo.class);
        //创建映射
        template.putMapping(SkuInfo.class);
    }

    //一键导入索引库
    @Override
    public void importAll() {
        //远程调用skuFegin里的方法,不加入spuId条件进行查询，查询的是审核通过的所有sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId("all");
        //判断是否为空
        if (skuList.size() <= 0 || skuList == null) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //将skuList转化为JSON形式
        String jsonSkuList = JSON.toJSONString(skuList);
        //将jsonSkuList转化为skuinfo形式
        List<SkuInfo> skuInfos = JSON.parseArray(jsonSkuList, SkuInfo.class);
        //遍历skuLists，封装为map集合
        for (SkuInfo skuInfo : skuInfos) {
            Map specMap = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(specMap);
        }
        esManagerMapper.saveAll(skuInfos);
    }

    //根据spuId同步数据到索引库中
    @Override
    public void importDataToESBySpuId(String spuId) {
        //远程调用skuFegin里的方法,不加入spuId条件进行查询，查询的是审核通过的所有sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        //判断是否为空
        if (skuList.size() <= 0 || skuList == null) {
            throw new RuntimeException("当前没有数据被查询到,无法导入索引库");
        }
        //将skuList转化为JSON形式
        String jsonSkuList = JSON.toJSONString(skuList);
        //将JSON转化为skuinfo
        List<SkuInfo> skuInfoList = JSON.parseArray(jsonSkuList, SkuInfo.class);
        //遍历封装为map集合
        for (SkuInfo skuInfo : skuInfoList) {
            Map map = JSON.parseObject(skuInfo.getSpec(), Map.class);
            skuInfo.setSpecMap(map);
        }
        esManagerMapper.saveAll(skuInfoList);
    }

    //根据souid删除es索引库中相关的sku数据
    @Override
    public void delDataBySpuId(String spuId) {
        //远程调用skuFegin里的方法,不加入spuId条件进行查询，查询的是审核通过的所有sku
        List<Sku> skuList = skuFeign.findSkuListBySpuId(spuId);
        //判断是否为空
        if (skuList==null||skuList.size()<=0){
            throw new RuntimeException("当前没有数据被查询到,无法删除索引库");
        }
        //循环删除
        for (Sku sku : skuList) {
            esManagerMapper.deleteById(Long.valueOf(sku.getId()));
        }
    }
}
